package com.android.delivery.util.print.task;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;

import com.android.delivery.entity.Logistics;
import com.android.delivery.util.print.LabelPrint;
import com.android.delivery.util.print.PrinterApi;
import com.android.delivery.widget.DragView;
import com.blankj.utilcode.util.LogUtils;
import com.lvrenyang.io.Pos;
import com.wisdom.tian.cpp.NativeUtil;

/**
 * Created by jc on 2017/11/18.
 */
@SuppressLint("NewApi")
public class TaskPosPrint implements Runnable {

    private static final String TAG = "TaskPosPrint";
    private Pos pos;
    private int printType = -1;
    private Handler mUiHandler = null;

    private Logistics mLogistics;

    public TaskPosPrint(Pos pos, Logistics logistics, Handler handler) {
        this.pos = pos;

        this.mLogistics = logistics;
        mUiHandler = handler;

        printType = -1;
    }

    public TaskPosPrint(Pos pos, DragView dragView, Handler handler) {
        this.pos = pos;

        mUiHandler = handler;

        printType = -1;
    }

    public TaskPosPrint(Pos pos, DragView dragView, Handler handler, int type) {
        this(pos, dragView, handler);

        printType = type;
    }

    // 发送更新BT状态的消息
    private void sendUiUpdateMessage(Object obj) {
        if (mUiHandler != null && obj != null) {
            Message message = new Message();
            message.what = 1;
            message.arg1 = 0;
            message.arg2 = 0;
            message.obj = obj;
            mUiHandler.sendMessage(message);
        }
    }

    // BT发送数据
    // send data to bluetooth
    private boolean sendBuffer(byte []data){
        return pos.POS_SendBuffer(data);
    }

    private int readBuffer(byte []data, int size, int timeout){
        return pos.POS_ReadBuffer(data, 0, size, timeout);
    }

    // CPCL Print Test
    // 测试PC标签调试工具，自动生成的CPCL代码
    private void CpclPrintTest(){
        byte []cmd = LabelPrint.CpclPrintTest();
        if (cmd != null && cmd.length > 0) {
            sendBuffer(cmd);
        }
    }

    private String hex2str(byte []dat, int size){
        String str = "\n";
        for (int i=0; i<size; i++){
            if (i != 0 && (i%16)==0)
                str += "\n";
            str += String.format("%02X ", dat[i]);
        }
        return str;
    }

    private String hex2str(byte []dat){
        return hex2str(dat, dat.length);
    }

    private void PrinterTest(){
        int no = 2;
        String str = "";
        byte []dat = null;

        // printer authorize
        // 获取认证指令
        dat = NativeUtil.LabelPrinterAuth();
        sendBuffer(dat);
        str += hex2str(dat);

        // label template download
        // 下载 标签模板
        String strTemplate = "! 0 203 203 1028 1\n" +
                            "XG 224 88 #*BEST*#\n" +
                            "TEXT 4 0 224 188 ###Text Demo!\n" +
                            "PRINT\n";
        dat = PrinterApi.getPrintStringGBKBytes(strTemplate);       // 模板数据
        dat = NativeUtil.LabelTemplateUpload(no, 1, dat);
        str += hex2str(dat);
        sendBuffer(dat);

        // label template print view
        // 打印预览上步下载的  标签模板
        dat = NativeUtil.LabelTemplatePreview(no);
        str += hex2str(dat);
        sendBuffer(dat);

        // logo download
        // LOGO 下载
        String strLogo = "FFFFFFFFF8FFFFFFF80FFFE00000000000000000007FFFFFFFF0FFFFFFF03FFFC00000000000000000007FFFFFFFF1FFFFFFE07FFF800000000000000000003FFFFFFFE1FFFFFFC0FFFF000000000000000000001FFFFFFFE3FFFFFF81FFFE000000000000000000000FFFFFFFC3FFFFFF03FFFC0000000000000000000007FFFFFFC7FFFFFE07FFF00000000000000000000003FFFFFF87FFFFFC0FFFE00000000000000000000001FFFFFF8FFFFFF83FFFC00000000000000000000000FFFFFF0FFFFFE07FFF800000000000000000000000FFFFFF1FFFFFC0FFFF0000000000000000000000000003FE1FFFE001FFFE0000000000000000000000000003FE3FFFC003FFF80000000000000000000000000007FC3FFF8007FFF00000000000000000000000000007FC7FFF801FFFE0000000000000000000000000000FF87FFF003FFFC0000000000000000000000000000FF8FFFF007FFF80000000000000000000000000001FF0FFFE00FFFF00000000000000000000000000001FF1FFFE01FFFC00000000000000000000000000003FE1FFFC03FFF800000000000000000000000000003FE3FFFC07FFF000000000000000000000000000007FC3FFF81FFFE000000000000000000000000000007FC7FFF83FFFC00000000000000000000000000000FF87FFF07FFF800000000000000000000000000000FF8FFFF0FFFE000000000000000000000000000001FF0FFFE1FFFC000000000000000000000000000001FF1FFFE3FFFC000007F0C38FC1FC1FC1F03E000003FE1FFFC3FFFC000007F0E70FE1FE1FC3F87F000003FE3FFFC1FFFE00000600760C718618031863000007FC3FFF80FFFF000006003E0C71861803C078000007FC7FFF807FFF800007F03C0FE1FC1FC3F07E00000FF87FFF007FFF800007F03C0FC1F81FC0780F00000FF8FFFF003FFFC00006007E0C019C18001C0380001FF0FFFE001FFFE0000600670C018E18071CE380001FF1FFFE000FFFF00007F0E30C018E1FC3F87F00003FE1FFFC000FFFF00007F0C38C01871FC0E01C00003FE3FFFC0007FFF8000000000000000000000000007FC3FFF80003FFFC000000000000000000000000007FC7FFF80001FFFE000FFFFC7FFFC0E1C038E78000FF8FFFF00001FFFE00001C0000E007EFFC01FFC001FF8FFFF00000FFFF00001C0000E007F1DC781CC001FF1FFFE000007FFF80001C0000E006E1DC79FFC003FF1FFFE000003FFFC0001C0000E006E1DC399C0003FE3FFFC000003FFFC00FFFFC7FFFC6E1DC399C0007FE3FFFC000001FFFE0003E0001F006E1DC39FFC007FC7FFF8000000FFFF0003E0001F006EFFC381CC00FFC7FFF80000007FFF800778003B806E1C0381CC00FF8FFFF00000003FFF800E1C00F1C06E3E038DDC01FF0FFFF00000003FFFC01C0E01C0E06E33039DC001FF1FFFE00000001FFFE038070380706E6183C00003FE1FFFE00000000FFFF070038700380EE1C3FFFC07FE3FFFE000000007FFF8E001CE001C0EC0E380000";
        dat = PrinterApi.getPrintStringGBKBytes(strLogo);
        dat = NativeUtil.LabelLogoUpload(no, 1, 168, 52, dat);
        sendBuffer(dat);
        str += hex2str(dat);

        // print data
        // 打印数据
        dat = NativeUtil.LabelLogoWithIndex(1);
        byte[] printDat = NativeUtil.LabelLogoWithName("POSTB");
        printDat = NativeUtil.addLabelTemplatePrintData(null, printDat);      // 第一次添加数据
        printDat = NativeUtil.addLabelTemplatePrintDataWithString(printDat, "Hello,World!", false);      // 第二次添加数据
        dat = NativeUtil.LabelTemplatePrint(no, printDat);      // 打印
        str += hex2str(dat);
        sendBuffer(dat);

        // check label template valid or invalid
        // 检查模板时候有效
        byte []cmd = NativeUtil.LabelTemplateCheck(no);
        str += hex2str(cmd);
        //sendBuffer(cmd);
        //int readSize = readBuffer(dat, dat.length, 1000);
        //str += hex2str(dat, readSize);

        LogUtils.e(TAG, str);

        sendUiUpdateMessage(str);
    }

    @Override
    public void run() {
        printCpclOrder(mLogistics);
    }

    private void printCpclOrder(Logistics logistics) {
        byte []cmd = LabelPrint.OrderPrint(logistics);
        if (cmd != null && cmd.length > 0) {
            sendBuffer(cmd);
        }
    }
}
